using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using PreguntaAlExperto.DataFramework;
using PreguntaAlExperto.RulesLayer.Entities;
using PreguntaAlExperto.TransporterObjectsFramework;
using PreguntaAlExperto.RulesLayer.ObjectMapper.Contracts;
using PreguntaAlExperto.RulesLayer.Services;

namespace PreguntaAlExperto.ObjectMapper.DTO
{
    internal class ArticlesDTO
        : BaseDTO
    {
        public object Select(string query, SearchCriteria searchCriteria)
        {
            DataAccess dataAccess = new DataAccess();
            BaseList<Article> _transporter = new BaseList<Article>();

            foreach (object rawEntity in dataAccess.Select(query, searchCriteria))
            {
                Article _article = new Article();
                _article.Article_ID = int.Parse(((ArrayList)rawEntity)[0].ToString());
                _article.Publisher_ID = int.Parse(((ArrayList)rawEntity)[1].ToString());
                _article.Path = ((ArrayList)rawEntity)[2].ToString();
                _article.Title = ((ArrayList)rawEntity)[3].ToString();
                _article.Short_Description = ((ArrayList)rawEntity)[4].ToString();
                _article.Publish_Date = (DateTime)((ArrayList)rawEntity)[5];
                _article.AllowedToPublish = (bool)((ArrayList)rawEntity)[6];
                _article.TotalVisits = int.Parse(((ArrayList)rawEntity)[7].ToString());
                _article.TotalPoints = int.Parse(((ArrayList)rawEntity)[8].ToString());
                _article.TotalVotes = int.Parse(((ArrayList)rawEntity)[9].ToString());

                _article.Platforms = GetPlatforms(_article);
                _article.Languages = GetLanguages(_article);
                _article.Technologies = GetTechnologies(_article);
                _article.Skills = GetSkills(_article);

                _article.Group_ID = GetGroupID(_article);

                _transporter.Add(_article);
            }
            _transporter.TotalRecords = dataAccess.getRecordsAfected;
            return _transporter;
        }

        private int GetGroupID(Article article)
        {
            object _element = new DataAccess().SelectOne("SQL_SELECT_GROUPID_FOR_ARTICLE", ByArticleId(article.Article_ID));
            if (_element != null)
                return int.Parse(_element.ToString());
            else
                return 0;
        }

        private BaseList<Pair> GetPlatforms(Article article)
        {
            return PairService.ToBaseList(
                new DataAccess().
                Select("SQL_SELECT_ARTICLE_PLATFORMS", ByArticleId(article.Article_ID))) ;  
        }

        private BaseList<Pair> GetLanguages(Article article)
        {
            return PairService.ToBaseList(
                new DataAccess().
                Select("SQL_SELECT_ARTICLE_LANGUAGES", ByArticleId(article.Article_ID) )) ;  
        }

        private BaseList<Pair> GetTechnologies(Article article)
        {
            return PairService.ToBaseList(
                new DataAccess().
                Select("SQL_SELECT_ARTICLE_TECHNOLOGIES", ByArticleId(article.Article_ID)));
        }

        private BaseList<Pair> GetSkills(Article article)
        {
            return PairService.ToBaseList(
                new DataAccess().
                Select("SQL_SELECT_ARTICLE_SKILLS", ByArticleId(article.Article_ID)));
        }

        private static SearchCriteria ByArticleId(int articleId)
        {
            SearchCriteria criteria = new SearchCriteria();
            criteria.addParameter("@ARTICLE_ID", articleId.ToString()); 
            return criteria;
        }
    }
}
